package com.starinside.wp.repository;

import com.starinside.wp.domain.ChildActivity;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;

public class ChildActivitySpecification {

//    public static Specification<ChildActivity> buildSpecification(String areaId, List<String> classIds) {
//        return (root, query, criteriaBuilder) -> {
//            List<Predicate> predicates = new ArrayList<>();
//
//            if (areaId != null && !areaId.isEmpty()) {
//                predicates.add(criteriaBuilder.equal(root.get("areaId"), areaId));
//            }
//
//            if (classIds != null && !classIds.isEmpty()) {
//                List<Predicate> classIdPredicates = new ArrayList<>();
//
//                for (String classId : classIds) {
//                    String trimmedClassId = classId.trim();
//                    // 确保匹配格式为 "1"、"1,2"、"3,1,4" 等无空格情况
//                    Predicate likeMiddle = criteriaBuilder.like(root.get("classIds"), "%," + trimmedClassId + ",%");
//                    Predicate likeStart = criteriaBuilder.like(root.get("classIds"), trimmedClassId + ",%");
//                    Predicate likeEnd = criteriaBuilder.like(root.get("classIds"), "%," + trimmedClassId);
//                    Predicate exactMatch = criteriaBuilder.equal(root.get("classIds"), trimmedClassId);
//
//                    Predicate combined = criteriaBuilder.or(likeMiddle, likeStart, likeEnd, exactMatch);
//                    classIdPredicates.add(combined);
//                }
//
//                predicates.add(criteriaBuilder.or(classIdPredicates.toArray(new Predicate[0])));
//            }
//
//            query.orderBy(criteriaBuilder.desc(root.get("createTime")));
//            return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
//        };
//    }


    public static Specification<ChildActivity> buildSpecification(String areaId, List<String> classIds) {
        return (root, query, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<>();

            if (areaId != null && !areaId.isEmpty()) {
                predicates.add(criteriaBuilder.equal(root.get("areaId"), areaId));
            }

            if (classIds != null && !classIds.isEmpty()) {
                List<Predicate> classPredicates = new ArrayList<>();
                for (String classId : classIds) {
                    String trimmedId = classId.trim();
                    Predicate findInSet = criteriaBuilder.greaterThan(
                            criteriaBuilder.function("FIND_IN_SET", Integer.class,
                                    criteriaBuilder.literal(trimmedId), root.get("classIds")),
                            0
                    );
                    classPredicates.add(findInSet);
                }
                predicates.add(criteriaBuilder.or(classPredicates.toArray(new Predicate[0])));
            }

            query.orderBy(criteriaBuilder.desc(root.get("createTime")));
            return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
        };
    }

}